package com.wswy.wzcx.ui.module.lbs

import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import com.che.libcommon.ui.NavTabController
import com.wswy.wzcx.R
import com.wswy.wzcx.module.base.CBaseFragment
import com.wswy.wzcx.ui.Constants
import kotlinx.android.synthetic.main.fragment_lbs_master.*
import timber.log.Timber

class LbsMasterFragment : CBaseFragment(),NavTabController.FragmentProvider {

    companion object {



        @JvmStatic
        fun newInstance(typeCode:Int = 1):CBaseFragment{

            return LbsMasterFragment().apply {

                arguments = Bundle().apply {
                    putInt(Constants.EXTRA_DATA_ID,typeCode)
                }

            }
        }

    }


    private lateinit var navTabController: NavTabController

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_lbs_master
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)


        Lbs.types.forEach {
            tabLayout.addTab(tabLayout.newTab().setText(it.name).setTag(it.type))
        }



        navTabController = NavTabController(this,childFragmentManager,R.id.fl_container)


        val id =  arguments?.getInt(Constants.EXTRA_DATA_ID)?:1

        val defType = Lbs.types.find { it.typeCode == id }?:Lbs.types.first()

        navTabController.show(defType.type)




        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }


            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {

                (tab?.tag as? String)?.let {

                    if (!navTabController.isShowing(it)){
                        navTabController.show(it)
                    }
                }

            }

        })

        val pos = Lbs.types.indexOfFirst { it.typeCode == defType.typeCode }

        if(pos >= 0){
            tabLayout.setScrollPosition(pos,0f,true)
        }


    }
    override fun getFragment(tag: String?): Fragment? {
        Timber.e("get -- $tag")

        return Lbs.types.find { it.type == tag }?.let {
             CommonLbsMapFragment.newInstance(it)
        }
    }


}